package com.example.core.license.service;

import com.example.core.license.config.SysLicenseParamConfig;
import de.schlichtherle.license.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.prefs.Preferences;

@Service
public class SysLicenseManagerImpl implements SysLicenseManager, InitializingBean {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private SysLicenseParamConfig sysLicenseParamConfig;

    @Value("${spring.profiles.active:}")
    private String active;

    @Override
    public LicenseParam initLicenseParam() {
        Preferences preferences = Preferences.userNodeForPackage(this.getClass());
        CipherParam cipherParam = new DefaultCipherParam(this.sysLicenseParamConfig.getStorePwd());
        KeyStoreParam keyStoreParam = new KeyStoreParam() {
            @Override
            public InputStream getStream() throws IOException {
                if (StringUtils.isEmpty(active) || active.equalsIgnoreCase("dev")) {
                    return this.getClass().getClassLoader().getResourceAsStream(sysLicenseParamConfig.getPublicKeyStore());
                } else if (active.equalsIgnoreCase("prod")) {
                    return new FileInputStream(sysLicenseParamConfig.getPublicKeyStore());
                } else {
                    throw new IOException("未知的环境变量指定: " + active);
                }
            }

            @Override
            public String getAlias() {
                return sysLicenseParamConfig.getPublicAlias();
            }

            @Override
            public String getStorePwd() {
                return sysLicenseParamConfig.getStorePwd();
            }

            @Override
            public String getKeyPwd() {
                return null;
            }
        };
        return new DefaultLicenseParam(this.sysLicenseParamConfig.getSubject(), preferences, keyStoreParam, cipherParam);
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        LicenseCustomManager licenseManager = new LicenseCustomManager(this.initLicenseParam());
        if (StringUtils.isEmpty(active) || active.equalsIgnoreCase("dev")) {
            URL url = this.getClass().getClassLoader().getResource(this.sysLicenseParamConfig.getLicensePath());
            licenseManager.install(new File(url.getPath()));
            LicenseContent licenseContent = licenseManager.verify();
        } else if (active.equalsIgnoreCase("prod")) {
            licenseManager.install(new File(this.sysLicenseParamConfig.getLicensePath()));
            LicenseContent licenseContent = licenseManager.verify();
        } else {
            throw new Exception("未知的环境变量指定: " + active);
        }
    }
}

